package com.hangwei.aicabinet.presenter;

import android.util.Log;

import com.hangwei.aicabinet.adapter.DoorColorAdapter;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.manager.SerialPortProxy;
import com.hangwei.aicabinet.model.bean.AdminOnAndOut;
import com.hangwei.aicabinet.model.bean.AdminUpLoad;
import com.hangwei.aicabinet.model.bean.DoorStatus;
import com.hangwei.aicabinet.model.entry.ColseDoorFinish;
import com.hangwei.aicabinet.model.entry.ColseDoorUpdateFail;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.util.LogUtil;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;
import com.hangwei.aicabinet.view.BaseView;

import org.greenrobot.eventbus.EventBus;

import rx.Observable;

import static com.hangwei.aicabinet.util.UpLoadUtil.getRFString;

/**
 * Created by 张龙臣 on 2018/10/31.
 */

public class LogoutPresenter extends BasePresenter {
    public LogoutPresenter(BaseView baseView) {
        super(baseView);
    }


    public void logout(final ViewCallBack viewCallBack) {
        if (viewCallBack != null)
            viewCallBack.onNetworkStart();
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                return NetManager.getResponseInfoAPI().logout(SPUtil.getString(SPKey.CABINET_ID), "XIANPADLOGIN");
            }

            @Override
            protected void onNext(String data) {
                if (viewCallBack != null)
                    viewCallBack.onNetworkComplete(true);
            }

            @Override
            protected void onError() {
                if (viewCallBack != null)
                    viewCallBack.onNetworkComplete(false);
            }
        });
    }

    public void updateLock(final ViewCallBack viewCallBack, final int status) {
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                int num = SPUtil.getInt(SPKey.Admin.OPEN_DOOR);
                String cabinet = SPUtil.getString(SPKey.CABINET_NUM);
                String spaceNum = "GD0001-";
                if (cabinet.startsWith("XA-GD001")){
                    spaceNum = "GD0001-";
                }
                if (cabinet.startsWith("XA-GD002")){
                    spaceNum = "GD0002-";
                }
                StringBuilder sb = new StringBuilder();
                sb.append(spaceNum);
                if (num < 10)
                    sb.append("0");
                sb.append(num);
                if (num < 10)
                    sb.append("-0");
                sb.append(num);
                String positionNum = sb.toString();
                Log.d("----------",positionNum);
                return NetManager.getResponseInfoAPI().updateLockStatus(positionNum, status);
            }

            @Override
            protected void onNext(String data) {
                LogUtil.d("关锁成功");
                if (viewCallBack != null) {
                    viewCallBack.onNetworkComplete(true);
                }
//                ToastUtil.showCenterToast("柜门已关闭");
//                SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                upLoadTab();
            }

            @Override
            protected void onError() {
                if (viewCallBack != null) {
                    viewCallBack.onNetworkComplete(false);
                }
            }
        });
    }

    public void closeDoorForAdmin() {
        NetManager.getInstance().doSubscribe(new NetManager.Model<AdminUpLoad>() {
            @Override
            protected Observable<ResponseInfo<AdminUpLoad>> getObservable() {
                int num = SPUtil.getInt(SPKey.Admin.OPEN_DOOR);
                String cabinet = SPUtil.getString(SPKey.CABINET_NUM);
                String spaceNum = "GD0001-";
                if (cabinet.startsWith("XA-GD001")){
                    spaceNum = "GD0001-";
                }
                if (cabinet.startsWith("XA-GD002")){
                    spaceNum = "GD0002-";
                }
                StringBuilder sb1 = new StringBuilder();
                sb1.append(spaceNum);
                if (num < 10)
                    sb1.append("0");
                sb1.append(num);
                if (num < 10)
                    sb1.append("-0");
                sb1.append(num);
                String positionNum = sb1.toString();
                Log.d("----------",positionNum);
                return NetManager.getResponseInfoAPI().closeDoorForAdmin(getRFString(SerialPortProxy.getAllTab()), positionNum);
            }

            @Override
            protected void onNext(AdminUpLoad data) {
                SPUtil.setInt(SPKey.Admin.OPEN_DOOR, -2);
                LogUtil.d("校验成功");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                ToastUtil.showCenterToast("工具检测完毕");
//                SPUtil.setString(SPKey.OPENED_DOOR_SPACE_NUM, "");
                EventBus.getDefault().post(new ColseDoorFinish());
            }

            protected void onError(String msg, int code) {
//                if (code == 2) {//归还错误
//                    Log.e("onError","归还错误-----------------");
//                    EventBus.getDefault().post(new ErrorReturnEvent(msg));
//                    if (App.tempLock != 0)
//                        updateLockStatus(1);
//                }else if (code == 5) {//归还错误  单柜
//                    Log.e("onError","归还错误-----------------");
//                    EventBus.getDefault().post(new ErrorReturnEvent(msg));
//                    if (App.tempLock != 0)
//                        updateLockStatus(1);
//                }
            }

            @Override
            protected void onError() {
                LogUtil.e("标签上传错误");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                EventBus.getDefault().post(new ColseDoorUpdateFail());
            }
        });
    }

    public void getAdminDoorView(final DoorColorAdapter doorColorAdapter) {
        NetManager.getInstance().doSubscribe(new NetManager.Model<DoorStatus>() {
            @Override
            protected Observable<ResponseInfo<DoorStatus>> getObservable() {
                return NetManager.getResponseInfoAPI().getBusySpace(SPUtil.getString(SPKey.CABINET_ID));
            }

            @Override
            protected void onNext(DoorStatus data) {
                doorColorAdapter.notifyDataSetChanged(data.spaceSize);
            }

            @Override
            protected void onError() {

            }
        });
    }
    
    public void toolsOnlineAndOutline(){
        NetManager.getInstance().doSubscribe(new NetManager.Model<AdminOnAndOut>() {
            @Override
            protected Observable<ResponseInfo<AdminOnAndOut>> getObservable() {
                int num = SPUtil.getInt(SPKey.Admin.OPEN_DOOR);
                String cabinet = SPUtil.getString(SPKey.CABINET_NUM);
                String spaceNum = "GD0001-";
                if (cabinet.startsWith("XA-GD001")){
                    spaceNum = "GD0001-";
                }
                if (cabinet.startsWith("XA-GD002")){
                    spaceNum = "GD0002-";
                }
                StringBuilder sb1 = new StringBuilder();
                sb1.append(spaceNum);
                if (num < 10)
                    sb1.append("0");
                sb1.append(num);
                if (num < 10)
                    sb1.append("-0");
                sb1.append(num);
                String positionNum = sb1.toString();
                Log.d("----------",positionNum);

                return NetManager.getResponseInfoAPI().toolsOnlineAndOutline(getRFString(SerialPortProxy.getAllTab()), positionNum);
            }

            @Override
            protected void onNext(AdminOnAndOut data) {
                SPUtil.setInt(SPKey.Admin.OPEN_DOOR, -2);
                LogUtil.d("校验成功");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                ToastUtil.showCenterToast("工具检测完毕");
//                SPUtil.setString(SPKey.OPENED_DOOR_SPACE_NUM, "");
                EventBus.getDefault().post(new ColseDoorFinish());
            }

            @Override
            protected void onError() {
                LogUtil.e("上传错误");
            }
        });
    }
}
